﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace psieve.sieve
{
    public class Eratosthenes : ISieve
    {
        public IEnumerable<long> Sieve(int end)
        {
            BitArray ray = new BitArray(end + 1, true);

            int sqrt = (int) Math.Sqrt(end);

            yield return 2;

            int p = 3;
            for (; p <= sqrt; p += 2)
            {
                if (ray[p])
                {
                    //yield return p;

                    for (int i = p * p; i <= end; i += 2 * p)
                        ray[i] = false;
                }
            }

            for (p = 3; p <= end; p += 2)
                if (ray[p])
                    yield return p;
        }
    }
}
